import datetime
import os
from ics import Calendar, Event
from enum import Enum
# from chinese_calendar.constants import holidays, in_lieu_days, workdays

def load_ics(ics_file,year):
    ics_file = open(ics_file, 'r').read()
    calendar = Calendar(ics_file)
    days = [i for i in calendar.events if i.begin.year == year]
    holidays,workdays =[],[]
    eHolidays = [i for i in days if "假期" in i.name]
    eWorkdays = [i for i in days if "上班" in i.name]
    for j in eHolidays:
        holidays.extend(getDays(j.begin,j.end))
    for j in eWorkdays:
        workdays.extend(getDays(j.begin,j.end))
    return holidays,workdays

def getDays(begin,end):
    ret = []
    i = begin
    ret.append(i.date())
    while i < end:
        i = i.replace(days=1)
    return ret

def is_workday(date):
    weekday = date.weekday()
    return bool(date in workdays or (weekday <= 4 and date not in holidays))

def getWorkDays(time_start,time_end):
    ret = 0.0
    day = time_start
    while day <= time_end:
        if is_workday(day):
            ret = ret + 1
        day = day + datetime.timedelta(days = 1)
    return ret

print(os.path.dirname(os.path.realpath(__file__)))
os.chdir(os.path.dirname(__file__))
holidays,workdays = load_ics("../2019.ics",2019)
